package com.yuetong.inventory.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.yuetong.common.utils.DateUtils;
import com.yuetong.common.utils.SecurityUtils;
import com.yuetong.inventory.domain.YtInventory;
import com.yuetong.inventory.mapper.YtInventoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yuetong.inventory.mapper.YtInventoryHistoryMapper;
import com.yuetong.inventory.domain.YtInventoryHistory;
import com.yuetong.inventory.service.IYtInventoryHistoryService;

/**
 * 出入库记录Service业务层处理
 *
 * @author  卓晓君
 * @date 2025-03-21
 */
@Service
public class YtInventoryHistoryServiceImpl implements IYtInventoryHistoryService
{
    @Autowired
    private YtInventoryHistoryMapper ytInventoryHistoryMapper;
    @Autowired
    private YtInventoryMapper ytInventoryMapper;

    /**
     * 查询出入库记录
     *
     * @param historyId 出入库记录主键
     * @return 出入库记录
     */
    @Override
    public YtInventoryHistory selectYtInventoryHistoryByHistoryId(Long historyId)
    {
        return ytInventoryHistoryMapper.selectYtInventoryHistoryByHistoryId(historyId);
    }

    /**
     * 查询出入库记录列表
     *
     * @param ytInventoryHistory 出入库记录
     * @return 出入库记录
     */
    @Override
    public List<YtInventoryHistory> selectYtInventoryHistoryList(YtInventoryHistory ytInventoryHistory)
    {
        return ytInventoryHistoryMapper.selectYtInventoryHistoryList(ytInventoryHistory);
    }

    /**
     * 新增出入库记录
     *
     * @param ytInventoryHistory 出入库记录
     * @return 结果
     */
    @Override
    public int insertYtInventoryHistory(YtInventoryHistory history)
    {   int fal = 0;
        YtInventory inventory = new YtInventory();
        inventory = ytInventoryMapper.selectYtInventoryByInventoryId(history.getInventoryId());
        history.setCreateTime(DateUtils.getNowDate());
        history.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        fal = ytInventoryHistoryMapper.insertYtInventoryHistory(history);
        if(fal == 1 && inventory != null){ // 获取库存并更新库存
            if(history.getChangeType().equals("IN")){
                // 计算库存
                BigDecimal afterQuantity = inventory.getCurrentStock().add(history.getQuantity());
                inventory.setCurrentStock(afterQuantity);
                // 计算金额
                BigDecimal amount = inventory.getPurchaseTotal().add(history.getAmount());
                inventory.setPurchaseTotal(amount);

            }else if(history.getChangeType().equals("OUT")){
                BigDecimal afterQuantity = inventory.getCurrentStock().add(history.getQuantity());
                inventory.setCurrentStock(afterQuantity);

            }
            // 更新信息
            inventory.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
            inventory.setUpdateTime(DateUtils.getNowDate());
            ytInventoryMapper.updateYtInventory(inventory);

        }
        return fal;
    }

    /**
     * 修改出入库记录
     *
     * @param ytInventoryHistory 出入库记录
     * @return 结果
     */
    @Override
    public int updateYtInventoryHistory(YtInventoryHistory history)
    {
        history.setUpdateTime(DateUtils.getNowDate());
        // 更新变更的人员信息
        history.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        history.setUpdateTime(DateUtils.getNowDate());

        // 更新库存记录
        int fal = 0;
        fal =  ytInventoryHistoryMapper.updateYtInventoryHistory(history);

        // 更新库存信息
        YtInventory inventory = new YtInventory();
        inventory = ytInventoryMapper.selectYtInventoryByInventoryId(history.getInventoryId());
        inventory.setUpdateBy(history.getCreateBy());
        inventory.setUpdateTime(DateUtils.getNowDate());
        if(fal == 1){
            if(history.getChangeType().equals("IN")){
                // 计算库存
                BigDecimal afterQuantity = inventory.getCurrentStock().add(history.getQuantity());
                inventory.setCurrentStock(afterQuantity);
                // 计算金额
                BigDecimal amount = inventory.getPurchaseTotal().add(history.getAmount());
                inventory.setPurchaseTotal(amount);

            }else if(history.getChangeType().equals("OUT")){
                BigDecimal afterQuantity = inventory.getCurrentStock().subtract(history.getQuantity());
                inventory.setCurrentStock(afterQuantity);

            }
            ytInventoryMapper.updateYtInventory(inventory);
        }
        return fal;
    }

    /**
     * 批量删除出入库记录
     *
     * @param historyIds 需要删除的出入库记录主键
     * @return 结果
     */
    @Override
    public int deleteYtInventoryHistoryByHistoryIds(Long[] historyIds)
    {
        int fal = 0;
        for (Long historyId : historyIds){
            YtInventoryHistory history = ytInventoryHistoryMapper.selectYtInventoryHistoryByHistoryId(historyId);
            YtInventory inventory = new YtInventory();
            inventory = ytInventoryMapper.selectYtInventoryByInventoryId(history.getInventoryId());
            BigDecimal afterQuantity = new BigDecimal(0);

            if(history.getChangeType().equals("IN")){
                BigDecimal amount = new BigDecimal(0);
                // 入库的就计算库存减库存
                 afterQuantity = inventory.getCurrentStock().subtract(history.getQuantity());
                 amount = inventory.getPurchaseTotal().subtract(history.getAmount());
                 inventory.setPurchaseTotal(amount);
            }else if(history.getChangeType().equals("OUT")){
                // 出库的就计算库存加库存
                 afterQuantity = inventory.getCurrentStock().subtract(history.getQuantity());
            }
            inventory.setCurrentStock(afterQuantity);


            // 更新库存
            fal = ytInventoryMapper.updateYtInventory(inventory);
            if (fal==1){
                // 删除记录
                ytInventoryHistoryMapper.deleteYtInventoryHistoryByHistoryId(historyId);
            }
        }
        return fal;
    }

    /**
     * 删除出入库记录信息
     *
     * @param historyId 出入库记录主键
     * @return 结果
     */
    @Override
    public int deleteYtInventoryHistoryByHistoryId(Long historyId)
    {
        // 查询出入库记录

        return ytInventoryHistoryMapper.deleteYtInventoryHistoryByHistoryId(historyId);
    }
}
